package control;

import entities.Folder;
import entities.TreeNode;
import java.io.IOException;
import java.util.GregorianCalendar;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import util.ServletUtil;

@WebServlet(name = "NewFolder", urlPatterns = {"/NewFolder"})
public class NewFolder extends HttpServlet {
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        boolean formSuccess = false;
        
        TreeNode folder = new Folder();
        
        request.setAttribute("headJsUrls", new String[]{"js/newFolder.js"});
        request.setAttribute("folder", folder);
        
        EntityManagerFactory fact = Persistence.createEntityManagerFactory("vh11_WebApplication1PU");
        EntityManager em = fact.createEntityManager();
        
        try {
            if(request.getMethod().equals("POST")) {
                
                // Zoek de parent node
                long parentId = ServletUtil.getLong(request.getParameter("parentId"), -1);
                TreeNode folderParent = em.find(TreeNode.class, parentId);
                
                // Maak de bean
                folder.setName(request.getParameter("name"));
                folder.setParent(folderParent);
                folder.setDateCreated(new GregorianCalendar());
                folder.setDateModified(new GregorianCalendar());
                
                try {
                    em.getTransaction().begin();
                    em.persist(folder);
                    em.getTransaction().commit();
                    formSuccess = true;
                }
                catch(ConstraintViolationException ex) {
                    request.setAttribute("formErrors", ex.getConstraintViolations());
                }
            }
        }
        finally {
            em.close();
            fact.close();
        }
        
        if (formSuccess) {
            response.sendRedirect("NewFolder?success=true");
        }
        else {
            RequestDispatcher dispatcher = request.getServletContext().getRequestDispatcher("/views/newFolder.jsp");
            dispatcher.forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
